<template>
  <a-modal
    title="编辑角色"
    :width="320"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          label="姓名"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['candidateName',
                                 {initialValue: candidate.candidateName,
                                  rules: [{required: true, message: '请输入候选人姓名', trigger: 'blur'}]}]" />
        </a-form-item>
        <a-form-item
          label="申报类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select v-decorator="['type', {initialValue: candidate.type}]">
            <a-select-option v-for="type in types" :key="type" >
              {{ type }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="申报职务"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-select v-decorator="['subject', {initialValue: candidate.subject}]">
            <a-select-option v-for="subject in subjects" :key="subject" >
              {{ subject }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="单位"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['dept', {initialValue: candidate.dept}]" />
        </a-form-item>
        <a-form-item
          label="职工号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['employeeNumber', {initialValue: candidate.employeeNumber}]" />
        </a-form-item>
        <a-form-item
          label="身份证号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input v-decorator="['idNumber', {initialValue: candidate.employeeNumber}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { editCandidate } from '@/api/vote/candidate'

export default {
  props: {
    types: {
      type: Array,
      default: () => {
        return []
      }
    },
    subjects: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      visible: false,
      confirmLoading: false,
      candidate: {},

      form: this.$form.createForm(this)
    }
  },
  methods: {
    edit (record) {
      this.visible = true
      this.candidate = record
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          editCandidate(Object.assign(values, { 'candidateId': this.candidate.candidateId }))
            .then(res => {
              if (res.message !== 'error') {
                this.visible = false
                this.confirmLoading = false
                this.form.resetFields()
                this.$emit('ok')
              } else {
                this.confirmLoading = false
                this.$message.error('操作失败！')
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
